package com.wing.product.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseProductionLine;
import com.wing.product.service.WarehouseProductionLineService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "产线管理", tags = "#/warehouse/product/warehouseProductionLine")
@RestController("WarehouseProductionLineController")
@RequestMapping("/warehouse/productionLine")
@RequiredArgsConstructor
public class WarehouseProductionLineController {

    private final WarehouseProductionLineService warehouseProductionLineService;

    @ApiOperation(value = "查询所有", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseProductionLine> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            Long workshopId,
            String productionLineCode,
            Long customerId
    ){
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
            customerId = JwtUtils.getDepartmentId();
        }
        Page<WarehouseProductionLine> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("workshopId", workshopId);
        paramMap.put("customerId", customerId);
        paramMap.put("productionLineCode", productionLineCode);

        //搜索
        IPage<WarehouseProductionLine> list = warehouseProductionLineService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加或修改产线", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/addOrUpdate")
    public JsonResult addOrUpdate(@RequestBody WarehouseProductionLine warehouseProductionLine)  {
        try{
            return warehouseProductionLineService.saveProductionLine(warehouseProductionLine);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "修改产线状态", notes = "修改")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/updateStatus")
    public JsonResult updateStatus(@RequestParam Long id, @RequestParam Integer status)  {
        try{
            return warehouseProductionLineService.updateStatus(id,status);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }
}
